home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / fwcp / src / termcap.c < prev    next >
C/C++ Source or Header  |  1994-03-22  |  8KB  |  408 lines

  1. /***
  2.  
  3.     termcap lib
  4.  
  5.     1994.3.18    make by ken
  6.  
  7. ***/
  8. #include    <stdio.h>
  9. #include    <stdlib.h>
  10. #include    <ctype.h>
  11. #include    <string.h>
  12.  
  13. #define    ERR        (-1)
  14. #define    FALSE        0
  15. #define    TRUE        1
  16.  
  17. #define    TERMCAP        termcap_file
  18.  
  19. extern    char    *termcap_file;
  20.  
  21. static    char    *tbuff = NULL;
  22.  
  23. void    tsetent(char *ent)
  24. {
  25.     tbuff = ent;
  26. }
  27. int    tgetent(char *buff, char *name)
  28. {
  29.     int n;
  30.     int ch;
  31.     int len;
  32.     FILE *fp;
  33.     char *p;
  34.  
  35.     tbuff = NULL;
  36.     len = strlen(name);
  37.  
  38.     if ( (p = TERMCAP) == NULL && (p = getenv("TERMCAP")) == NULL )
  39.     return ERR;
  40.     if ( (fp = fopen(p, "r")) == NULL )
  41.     return ERR;
  42.  
  43.     for ( ; ; ) {
  44.     n = 0;
  45.     while ( (ch = getc(fp)) != EOF ) {
  46.         if ( ch == '\n' ) {
  47.         if ( n > 0 && buff[n - 1] == '\\' ) {
  48.             n--;
  49.             continue;
  50.         }
  51.         break;
  52.         } else if ( n == 0 && ch == '#' ) {
  53.         while ( (ch = getc(fp)) != EOF && ch != '\n' )
  54.             ;
  55.         ungetc(ch, fp);
  56.         } else if ( n < BUFSIZ )
  57.         buff[n++] = ch;
  58.     }
  59.     buff[n] = '\0';
  60.  
  61.     if ( n == 0 && ch == EOF ) {
  62.         fclose(fp);
  63.         return FALSE;
  64.     }
  65.  
  66.     if ( strncmp(buff, name, len) == 0 &&
  67.         (buff[len] == ':' || buff[len] == '|') ) {
  68.         fclose(fp);
  69.         tbuff = buff;
  70.         return TRUE;
  71.     }
  72.     }
  73. }
  74. static    char    *tgetd(char *str, int *val)
  75. {
  76.     int n;
  77.     int ch = 0;
  78.  
  79.     if ( *str == 'x' ) {
  80.     str++;
  81.     for ( n = 0 ; n < 2 && isxdigit(*str) ; n++ ) {
  82.         if ( isdigit(*str) )
  83.         ch = ch * 16 + *str - '0';
  84.         else
  85.         ch = ch * 16 + toupper(*str) - 'A' + 10;
  86.         str++;
  87.     }
  88.     } else if ( *str == '0' ) {
  89.     str++;
  90.     for ( n = 0 ; n < 3 && *str >= '0' && *str <= '7' ; n++ )
  91.         ch = ch * 8 + *(str++) - '0';
  92.     } else if ( isdigit(*str) ) {
  93.     for ( n = 0 ; n < 3 && isdigit(*str) ; n++ )
  94.         ch = ch * 10 + *(str++) - '0';
  95.     } else
  96.     ch = *(str++);
  97.     *val = ch;
  98.     return str;
  99. }
  100. static    char    *tgetc(char *str, int *val)
  101. {
  102.     int n;
  103.     int ch;
  104.  
  105.     switch(*str) {
  106.     case '\\':
  107.     str++;
  108.     switch(*str) {
  109.     case 'E': ch = '\033'; str++; break;
  110.     case 'F': ch = '\034'; str++; break;
  111.     case 'G': ch = '\035'; str++; break;
  112.     case 'H': ch = '\036'; str++; break;
  113.     case 'I': ch = '\037'; str++; break;
  114.     case 'b': ch = '\010'; str++; break;
  115.     case 't': ch = '\011'; str++; break;
  116.     case 'n': ch = '\012'; str++; break;
  117.     case 'h': ch = '\013'; str++; break;
  118.     case 'c': ch = '\014'; str++; break;
  119.     case 'r': ch = '\015'; str++; break;
  120.     case 'e': ch = '\016'; str++; break;
  121.     case 'f': ch = '\017'; str++; break;
  122.     default: str = tgetd(str, &ch); break;
  123.     }
  124.     break;
  125.     case '^':
  126.     str++;
  127.     if ( *str >= 'A' && *str <= 'Z' )
  128.         ch = *str - '@';
  129.     else
  130.         ch = '^';
  131.     break;
  132.     default:
  133.     ch = *(str++);
  134.     break;
  135.     }
  136.  
  137.     *val = ch;
  138.     return str;
  139. }
  140. static    char    *tskip(char *p)
  141. {
  142.     int n;
  143.  
  144.     while ( *p != '\0' ) {
  145.     if ( *p == ':' )
  146.         return ++p;
  147.     p = tgetc(p, &n);
  148.     }
  149.     return p;
  150. }
  151. int    tgetnum(char *id)
  152. {
  153.     int n;
  154.     char *p;
  155.  
  156.     if ( (p = tbuff) == NULL )
  157.     return ERR;
  158.  
  159.     for ( ; ; ) {
  160.     p = tskip(p);
  161.     if ( *p == '\0' )
  162.         return ERR;
  163.     if ( p[0] != id[0] || p[1] != id[1] || p[2] != '#' )
  164.         continue;
  165.     p += 3;
  166.     if ( *p == '0' ) {
  167.         for ( n = 0 ; *p >= 0 && *p <= '7' ; p++ )
  168.         n = n * 8 + *p - '0';
  169.     } else {
  170.         for ( n = 0 ; isdigit(*p) ; p++ )
  171.         n = n * 10 + *p - '0';
  172.     }
  173.     return n;
  174.     }
  175. }
  176. int    tgetflag(char *id)
  177. {
  178.     char *p;
  179.  
  180.     if ( (p = tbuff) == NULL )
  181.     return ERR;
  182.  
  183.     for ( ; ; ) {
  184.     p = tskip(p);
  185.     if ( *p == '\0' )
  186.         return ERR;
  187.     if ( p[0] != id[0] || p[1] != id[1] )
  188.         continue;
  189.  
  190.     if ( p[2] == ':' )
  191.         return TRUE;
  192.     else if ( p[2] == '@' )
  193.         return FALSE;
  194.     }
  195. }
  196. char    *tgetstr(char *id, char **area)
  197. {
  198.     int n;
  199.     char *p;
  200.     char *s;
  201.  
  202.     if ( (p = tbuff) == NULL )
  203.     return NULL;
  204.  
  205.     for ( ; ; ) {
  206.     p = tskip(p);
  207.     if ( *p == '\0' )
  208.         return NULL;
  209.     if ( p[0] != id[0] || p[1] != id[1] )
  210.         continue;
  211.  
  212.     if ( p[2] == '@' )
  213.         return NULL;
  214.     else if ( p[2] != '=' )
  215.         continue;
  216.     p += 3;
  217.  
  218.     s = *area;
  219.     while ( *p != '\0' && *p != ':' ) {
  220.         p = tgetc(p, &n);
  221.         *(s++) = n;
  222.     }
  223.     *(s++) = '\0';
  224.     p = *area;
  225.     *area = s;
  226.     return p;
  227.     }
  228. }
  229. void    tputs(char *str, int ac, void (*put)(int ch))
  230. {
  231.     if ( ac < 1 || put == (void (*))0 )
  232.     return;
  233.     while ( *str != '\0' )
  234.     (*put)(*(str++));
  235. }
  236. /****************
  237.     %%                    %
  238.     %[[:]+-# ][0-9[.0-9]][doxXs]        pop printf
  239.     %c                    pop
  240.     %p[1-9]                    push [1-9]
  241.     %P[a-z]                    push [a-z]
  242.     %g[a-z]                    pop [a-z]
  243.     %'c'                    push 'c'
  244.     %{0-9}                    push {0-9}
  245.     %l                    strlen pop
  246.     %+ %- %* %/ %m                +,-,*,/,% pop ? pop push
  247.     %& %| %^                &,|,^ pop ? pop push
  248.     %i                    inc
  249.  
  250. not support
  251.     %= %> %<                =,>,< pop ? pop push
  252.     %A %O                    &&,|| pop ? pop push
  253.     %! %~                    pop ? push
  254.     %? ... %t ... %e ... %;            if then else fi
  255. *****************/
  256.  
  257. #define    STKSIZ    16
  258.  
  259. static    int    inchar(int ch, char *ptn)
  260. {
  261.     int n;
  262.     for ( n = 0 ; ptn[n] != '\0' ; n++ ) {
  263.     if ( ch == ptn[n] )
  264.         return n;
  265.     }
  266.     return (-1);
  267. }
  268. char    *tgoto(char *str, int x, int y)
  269. {
  270.     int n;
  271.     int len = 0;
  272.     int ent = 0;
  273.     int stk[STKSIZ];
  274.     char stc[STKSIZ];
  275.     char dmy[32];
  276.     static int dat[26];
  277.     static char tmp[BUFSIZ + 2];
  278.  
  279.     while ( ent < STKSIZ && len < BUFSIZ && *str != 0 ) {
  280.     if ( *str == '%' ) {
  281.         switch(*(++str)) {
  282.         case '%':
  283.         tmp[len++] = *(str++);
  284.         break;
  285.         case 'c':
  286.         tmp[len++] = (char)(stk[--ent]);
  287.         str++;
  288.         break;
  289.         case 'p':
  290.         switch(str[1]) {
  291.         case '1': stk[ent++] = x; break;
  292.         case '2': stk[ent++] = y; break;
  293.         }
  294.         str += 2;
  295.         break;
  296.         case 'P':
  297.         dat[str[1] - 'a'] = stk[--ent];
  298.         str += 2;
  299.         break;
  300.         case 'g':
  301.         stk[ent++] = dat[str[1] - 'a'];
  302.         str += 2;
  303.         break;
  304.         case '\'':
  305.         str = tgetc(str + 1, &n);
  306.         stk[ent++] = n;
  307.         break;
  308.         case '{':
  309.         str = tgetd(str + 1, &n);
  310.         stk[ent++] = n;
  311.         if ( *str == '}' )
  312.             str++;
  313.         break;
  314.         case 'l':
  315.         stk[ent] = ent;
  316.         ent++;
  317.         str++;
  318.         break;
  319.         case '+':
  320.         stk[ent-2] = stk[ent-2] + stk[ent-1];
  321.         ent--;
  322.         str++;
  323.         break;
  324.         case '-':
  325.         stk[ent-2] = stk[ent-2] - stk[ent-1];
  326.         ent--;
  327.         str++;
  328.         break;
  329.         case '*':
  330.         stk[ent-2] = stk[ent-2] * stk[ent-1];
  331.         ent--;
  332.         str++;
  333.         break;
  334.         case '/':
  335.         stk[ent-2] = stk[ent-2] / stk[ent-1];
  336.         ent--;
  337.         str++;
  338.         break;
  339.         case 'm':
  340.         stk[ent-2] = stk[ent-2] % stk[ent-1];
  341.         ent--;
  342.         str++;
  343.         break;
  344.         case '&':
  345.         stk[ent-2] = stk[ent-2] & stk[ent-1];
  346.         ent--;
  347.         str++;
  348.         break;
  349.         case '|':
  350.         stk[ent-2] = stk[ent-2] | stk[ent-1];
  351.         ent--;
  352.         str++;
  353.         break;
  354.         case '^':
  355.         stk[ent-2] = stk[ent-2] ^ stk[ent-1];
  356.         ent--;
  357.         str++;
  358.         break;
  359.         case '~':
  360.         stk[ent-1] = ~(stk[ent-1]);
  361.         str++;
  362.         break;
  363.         case 'i':
  364.         x += 1;
  365.         y += 1;
  366.         str++;
  367.         break;
  368.         default:
  369.         n = 0;
  370.         dmy[n++] = '%';
  371.         tmp[len] = '\0';
  372.  
  373.         if ( *str == ':' )
  374.             str++;
  375.         if ( inchar(*str, "+-# ") >= 0 )
  376.             dmy[n++] = *(str++);
  377.         while ( isdigit(*str) )
  378.             dmy[n++] = *(str++);
  379.         if ( *str == '.' )
  380.             dmy[n++] = *(str++);
  381.         while ( isdigit(*str) )
  382.             dmy[n++] = *(str++);
  383.  
  384.         if ( inchar(*str, "doxX") >= 0 ) {
  385.             dmy[n++] = *(str++);
  386.             dmy[n] = '\0';
  387.             sprintf(tmp + len, dmy, stk[--ent]);
  388.         } else if ( *str == 's' ) {
  389.             dmy[n++] = *(str++);
  390.             dmy[n] = '\0';
  391.             for ( n = 0 ; n < ent ; n++ )
  392.             stc[n] = (char)(stk[n]);
  393.             stc[n] = '\0';
  394.             sprintf(tmp + len, dmy, stc);
  395.             ent = 0;
  396.         }
  397.  
  398.         while ( tmp[len] != '\0' )
  399.             len++;
  400.         break;
  401.         }
  402.     } else
  403.         tmp[len++] = *(str++);
  404.     }
  405.     tmp[len] = '\0';
  406.     return tmp;
  407. }
  408.